# @Version        : 1.0
# @Update Time    : 2025/3/14 14:36
# @File           : category_view.py
# @IDE            : PyCharm
# @Desc           : 文件描述信息
import flet as ft


class CategoryView(ft.Column):
    def __init__(self, page: ft.Page, params: dict = None):
        super().__init__()
        self.page = page
        self.params = params
        self.expand = True
        self.build_interface()

    def build_interface(self):
        # 顶部导航栏
        app_bar = ft.Container(
            content=ft.Row(
                controls=[
                    ft.Text("全部分类", size=20, weight=ft.FontWeight.BOLD),
                    ft.Container(width=40)  # 占位保持对称
                ],
                alignment=ft.MainAxisAlignment.SPACE_BETWEEN
            ),
            padding=ft.padding.symmetric(vertical=15, horizontal=20)
        )

        # 分类说明文字
        subtitle = ft.Container(
            content=ft.Text(
                "按客户需求贴近人",
                color=ft.colors.GREY_600,
                size=14
            ),
            padding=ft.padding.only(left=20, bottom=10)
        )

        # 分类项目网格布局
        self.categories = ft.GridView(
            runs_count=4,  # 初始值
            spacing=50,
            run_spacing=30,
            max_extent=80,
            controls=[
                self.create_category_item(
                    icon=ft.icons.APPLE,
                    color=ft.colors.ORANGE_400,
                    title="水果"
                ),
                self.create_category_item(
                    icon=ft.icons.GRASS,
                    color=ft.colors.GREEN_400,
                    title="蔬菜"
                ),
                self.create_category_item(
                    icon=ft.icons.BREAKFAST_DINING,
                    color=ft.colors.BROWN_400,
                    title="面包"
                ),
                self.create_category_item(
                    icon=ft.icons.SET_MEAL,
                    color=ft.colors.RED_400,
                    title="鲜肉"
                ),
                self.create_category_item(
                    icon=ft.icons.LOCAL_FIRE_DEPARTMENT,
                    color=ft.colors.PINK_400,
                    title="5B相食品"
                ),
                self.create_category_item(
                    icon=ft.icons.CAKE,
                    color=ft.colors.YELLOW_400,
                    title="奶酪"
                ),
                self.create_category_item(
                    icon=ft.icons.LOCAL_DRINK,
                    color=ft.colors.BLUE_400,
                    title="饮料"
                )
            ]
        )

        self.controls = [
            app_bar,
            subtitle,
            ft.Container(self.categories, expand=True)
        ]

    def on_resize(self, e):
        # 根据页面宽度动态调整 runs_count
        window_width = self.page.window_width
        if window_width < 600:
            self.categories.runs_count = 2
        else:
            self.categories.runs_count = 3
        self.categories.update()

    def create_category_item(self, icon, color, title):
        return ft.Container(
            content=ft.Column(
                controls=[
                    ft.Container(
                        content=ft.Icon(icon, color=ft.colors.WHITE, size=30),
                        width=80,
                        height=80,
                        bgcolor=color,
                        border_radius=15,
                        alignment=ft.alignment.center
                    ),
                    ft.Text(
                        title,
                        color=ft.colors.GREY_800,
                        weight=ft.FontWeight.W_500
                    )
                ],
                horizontal_alignment=ft.CrossAxisAlignment.CENTER,
                spacing=10
            ),
            alignment=ft.alignment.center
        )
